/*
 * ExtTableException.java
 *
 * $Id: ExtTableException.java,v 1.1 2008-03-18 12:58:45 mario Exp $
 *
 * Written by: Mario García García <mario@imagos.es>
 * (c) 2007 Fundación Centro Tecnológico de la Carne
 * 
 */
package org.ceteca.explica.core.extTables;

/**
 * This class encapsulates and holds all the errors that might have taken
 * place during any of the extension table operations.
 * <br/>
 * Responsibilities:
 * <li>
 * 	<ul> Encapsulate and pass on the error messages to the upper layers. </ul>
 * </li>
 */
public class ExtTableException extends Exception {
	private static final long serialVersionUID = 10000009;
	
	/**
	 * Code of the extension field involved in the operation that
	 * originated the error.
	 */
	private String codExtField;
	/**
	 * Code of the extension table  involved in the operation that
	 * originated the error.
	 */
	private String codExtTable;
	/**
	 * Id of the extended item involved in the operation that
	 * originated the error. 
	 */
	private long idItem;
	/**
	 * Internal error code to support error localization and classification.
	 */
	private int errCode;
	/**
	 * Error message string that is showed to the end user.
	 */
	private String errMessage;

	public ExtTableException(int errCode) {
		super();
		this.errCode = errCode;
		this.codExtField = null;
		this.codExtTable = null;
		this.idItem = -1;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	public ExtTableException(int errCode, Throwable cause) {
		super(cause);
		this.errCode = errCode;
		this.codExtField = null;
		this.codExtTable = null;
		this.idItem = -1;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	public ExtTableException(String codExtTable, int errCode) {
		super();
		this.errCode = errCode;
		this.codExtTable = codExtTable;
		this.codExtField = null;
		this.idItem = -1;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	public ExtTableException(String codExtTable, int errCode, Throwable cause) {
		super(cause);
		this.errCode = errCode;
		this.codExtTable = codExtTable;
		this.codExtField = null;
		this.idItem = -1;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	public ExtTableException(String codExtTable, String codExtField, int errCode) {
		super();
		this.errCode = errCode;
		this.codExtTable = codExtTable;
		this.codExtField = codExtField;
		this.idItem = -1;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	public ExtTableException(String codExtTable, String codExtField, int errCode, Throwable cause) {
		super(cause);
		this.errCode = errCode;
		this.codExtTable = codExtTable;
		this.codExtField = codExtField;
		this.idItem = -1;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	public ExtTableException(String codExtTable, String codExtField, long idItem, int errCode) {
		super();
		this.errCode = errCode;
		this.codExtTable = codExtTable;
		this.codExtField = codExtField;
		this.idItem = idItem;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	public ExtTableException(String codExtTable, String codExtField, long idItem, int errCode, Throwable cause) {
		super(cause);
		this.errCode = errCode;
		this.codExtTable = codExtTable;
		this.codExtField = codExtField;
		this.idItem = idItem;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	public String getCodExtField() {
		return codExtField;
	}

	public void setCodExtField(String codExtField) {
		this.codExtField = codExtField;
	}

	public String getCodExtTable() {
		return codExtTable;
	}

	public void setCodExtTable(String codExtTable) {
		this.codExtTable = codExtTable;
	}

	public long getIdItem() {
		return idItem;
	}

	public void setIdItem(long idItem) {
		this.idItem = idItem;
	}

	public void setErrMessage(String errMessage) {
		this.errMessage = errMessage;
	}
	
	public String getErrMessage() {
		return errMessage;
	}
	
	public String getMessage() {
		return this.toString();
	}

	public String toString() {
		StringBuffer msg = new StringBuffer();
		if (this.codExtTable != null && this.codExtTable.length() > 0) {
			msg.append("[");
			msg.append(this.codExtTable);
			msg.append("]");
		}
		if (this.codExtField != null && this.codExtField.length() > 0) {
			msg.append("\t");
			msg.append(this.codExtField);
		}
		if (this.idItem > 0) {
			msg.append(" - ");
			msg.append(this.idItem);
		}
		
		return msg.toString();
	}

	public int getErrCode() {
		return errCode;
	}

	public void setErrCode(int errCode) {
		this.errCode = errCode;
	}
	
	/**
	 * Get the localized error message corresponding to the error code.
	 * @param errCode int, error code.
	 * @return String, localized error message corresponding to the error code.
	 */
	private String getErrorMessage(int errCode) {
		StringBuffer msg = new StringBuffer();
		
		// TO_DO: Obtener el correspondiente mensaje para ese codigo de error
		return msg.toString();
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: ExtTableException.java,v 1.1 2008-03-18 12:58:45 mario Exp $";
	}
}
